<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               ref="crud"
               v-model="form"
               :permission="permissionList"
               :before-open="beforeOpen"
               @row-del="rowDel"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <!--<template slot="menuLeft">-->
        <!--<el-button type="danger"-->
                   <!--size="small"-->
                   <!--icon="el-icon-delete"-->
                   <!--v-if="permission.dict_delete"-->
                   <!--plain-->
                   <!--@click="handleDelete">删 除-->
        <!--</el-button>-->
      <!--</template>-->
      <template slot-scope="{row}"
                slot="deptId">
        <el-tag>{{row.deptName}}</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="siteId">
        {{row.siteName}}
      </template>
      <template slot-scope="{row}"
                slot="status">
        <el-tag  type="success" v-if="+row.status === 1">成功</el-tag>
        <el-tag  type="danger" v-else-if="+row.status === 2">失败</el-tag>
        <el-tag   type="info" v-else>无数据</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="fullStatus">
        <el-tag  type="success" v-if="+row.fullStatus === 1">正常</el-tag>
        <el-tag  type="danger" v-else-if="+row.healthy === 2">满溢</el-tag>
        <el-tag   type="info" v-else>无数据</el-tag>
      </template>

      <template slot-scope="scope" slot="menu">
        <span class="view_ckeck"  @click="viewCheckBoxAction(scope.row)">安装包检查</span>
        <span class="view_ckeck"  @click="viewCheckBoxAction(scope.row)">升级</span>
        <span class="view_ckeck"  @click="viewCheckBoxAction(scope.row)">恢复初始版本</span>
        <!--<el-button icon="el-icon-check" size="small" @click="viewCheckBoxAction(scope.row)">查看</el-button>-->
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {add, getDict, getDictTree, getList, remove, update} from "@/api/device/firmware";
  import {mapGetters} from "vuex";

  export default {
    name: 'firmware',
    data() {
      return {
        form: {},
        selectionList: [],
        loading: true,
        query: {},
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        option: {
          refreshBtn: false,
          searchShow: false,
          tip: false,
          tree: true,
          border: true,
          index: true,
          selection: false,
          editBtn: false,
          delBtn: false,
          addBtn: false,
          menuWidth: 200,
          dialogCloseBtn: true,
          columnBtn: false,
          search: false,
          column: [
            {
              label: "所属组织",
              prop: "deptId",
              type: "select",
              search: false,
              dicUrl: "/api/blade-system/dept/tree",
              props: {
                label: "title",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请选择所属组织",
                trigger: "blur"
              }],
              span: 12,
              width: 120,
              slot:true,
              cascaderChange: true,
              cascaderItem: ['siteId'],
            },
            {
              label: "负责站点",
              prop: "siteId",
              search: false,
              selection:true,
              type: "tree", //注意 这里会触发刷新！！！！！！坑
//              multiple: true,
              dicFlag: false,
              defaultIndex: 1,
              dicUrl: "/api/blade-system/site/select?deptId={{key}}",
              props: {
                label: "siteName",
                value: "id"
              },
              slot:true,
              span: 12,
            },
            {
              label: "设备编号",
              prop: "deviceCode",
              search: false,
              addDisplay: false,
              editDisplay:false,
            },
            {
              label: "设备名称",
              prop: "deviceName",
              search: false,
              addDisplay: false,
              editDisplay:false,
            },
            {
              label: "当前版本号",
              prop: "version",
              search: false,
              addDisplay: false,
              editDisplay:false,
            },
            {
              label: "上次升级时间",
              prop: "updateTime",
              search: false,
              addDisplay: false,
              editDisplay:false,
            },
            {
              label: "设备状态",
              prop: "status",
              type: "select",
              dicData: [
                {
                  label: "成功",
                  value: 1
                },
                {
                  label: "失败",
                  value: 2
                }
              ],
              search: false,
              addDisplay: false,
              editDisplay: false,
              slot: true,
            },
          ]
        },
        data: []
      };
    },
    props: ["firmwareRow"],
    computed: {
      ...mapGetters(["userInfo", "permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.dict_add, false),
          viewBtn: this.vaildData(this.permission.dict_view, false),
          delBtn: this.vaildData(this.permission.dict_delete, false),
          editBtn: this.vaildData(this.permission.dict_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      viewCheckBoxAction() {
        this.$message({
          type: "success",
          message: "待补充"
        });
      },
      handleAdd(row) {
        this.$refs.crud.value.code = row.code;
        this.$refs.crud.value.parentId = row.id;
        this.$refs.crud.option.column.filter(item => {
          if (item.prop === "code") {
            item.value = row.code;
            item.addDisabled = true;
          }
          if (item.prop === "parentId") {
            item.value = row.id;
            item.addDisabled = true;
          }
        });
        this.$refs.crud.rowAdd();
      },
      rowSave(row, done, loading) {
        row.trueCapacity = parseFloat(row.trueCapacity) >0 ? parseFloat(row.trueCapacity) : 0
        row.garbageSortDeviceId =  this.timerRow.id
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        row.trueCapacity = parseFloat(row.trueCapacity) >0 ? parseFloat(row.trueCapacity) : 0
        row.garbageSortDeviceId =  this.timerRow.id
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDict(this.form.id).then(res => {
            this.form = res.data;
          });
        }
        done();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, {deviceId: this.firmwareRow.id}).then(res => {
          this.data = res.data.records;
          this.loading = false;
          console.log(this.data)
//          getDictTree().then(res => {
//            const column = this.findObject(this.option.column, "parentId");
//            column.dicData = res.data;
//          });
        });
      }
    }
  };
</script>

<style>
</style>
